package com.example.java_chatroom.api;

import com.example.java_chatroom.mapper.FriendMapper;
import com.example.java_chatroom.model.Friend;
import com.example.java_chatroom.model.MessageResponse;
import com.example.java_chatroom.model.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class FriendAPI {
    @Resource
    private FriendMapper friendMapper;

    @GetMapping("/friendList")
    @ResponseBody
    public Object getFriendList(HttpServletRequest req) {
        //1.先从会话中获取到 userId
        HttpSession session = req.getSession(false);
        if(session == null) {
            //会话不存在
            log.info("session 不存在");
            return new ArrayList<Friend>();
        }
        User user = (User) session.getAttribute("user");
        if(user == null) {
            //当前用户对象没在会话中存在
            log.info("user 不存在");
            return new ArrayList<Friend>();
        }
        //2.根据userId 从数据库查数据
        List<Friend> friendList = friendMapper.selectFriendList(user.getUserId());
        return friendList;
    }

    @GetMapping("/searchFriend")
    @ResponseBody
    public Object SearchFriend(String friendName, @SessionAttribute("user") User user) {
        if(friendName == null || friendName.equals("")) {
            return new ArrayList<Friend>();
        }
        //用户列表中还要排除自己
        List<Friend> friendList = friendMapper.searchFriendListByName(friendName, user.getUserId());
        for(Friend friend : friendList) {
            // 检查是否已经为好友关系
            int res1 = friendMapper.checkFriend(user.getUserId(), friend.getFriendId());
            //检查是否向对方发送过好友申请
            int res2 = friendMapper.checkNewFriendApplication(user.getUserId(), friend.getFriendId());
            if(res1 > 0) {
                //是好友
                friend.setStatus(1);
            }else if(res2 > 0){
                //发送过好友申请
                friend.setStatus(0);
            }else {
                //不是好友, 也没有发送过好友申请
                friend.setStatus(2);
            }
        }
        return friendList;
    }

    /**
     * 查询好友申请信息
     * @param user
     * @return
     */
    @GetMapping("/newFriendApplication")
    @ResponseBody
    public Object newFriendApplication(@SessionAttribute("user") User user) {
        List<MessageResponse> messageResponses = friendMapper.selectNewFriendApplication(user.getUserId());
        for(MessageResponse message : messageResponses) {
            message.setType("newFriend");
        }
//        System.out.println("========================================");
//        System.out.println(messageResponses.toString());
        return messageResponses;
    }

    //拒绝好友请求
    @PostMapping("/rejectApplication")
    public Boolean rejectApplication(int friendId, @SessionAttribute("user") User user) {
        log.info("拒绝好友请求, userId:" + user.getUserId() + ", friendId: " + friendId);
        try {
            friendMapper.deleteFriendApplication(friendId, user.getUserId());
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //同意好友请求
    @PostMapping("/acceptApplication")
    public Boolean acceptApplication(int friendId, @SessionAttribute("user") User user) {
        log.info("接收好友请求, userId:" + user.getUserId() + ", friendId: " + friendId);
        // 检查是否已经为好友关系
        int res = friendMapper.checkFriend(user.getUserId(), friendId);
        if(res > 0) {
            //已经是好友关系了
            return true;
        }
        try {
            friendMapper.addFriend(user.getUserId(), friendId);
            friendMapper.addFriend(friendId, user.getUserId());
            friendMapper.deleteFriendApplication(friendId, user.getUserId());
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
